package com.plm.blogs.utils;

import lombok.extern.slf4j.Slf4j;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * @author Limi
 * @create 2019-01-12 21:05
 */
@Slf4j
public class EmailTool {

    /**
     * 发件人 账号和密码
     */
    public static final String MY_EMAIL_ACCOUNT = "maque_blogs@163.com";
    /**
     * 密码,是你自己的设置的授权码,
     * 获取方式：登陆网页邮箱，设置-》客户端授权密码
     */
    public static final String MY_EMAIL_PASSWORD = "java1314";

    /**
     * SMTP服务器(这里用的163 SMTP服务器)
     */
    public static final String MEAIL_163_SMTP_HOST = "smtp.163.com";
    /**
     * 端口号,这个是163使用到的
     */
    public static final String SMTP_163_PORT = "25";

    /**
     * @param receiveEmail 收件人
     * @param content 内容
     * @param action 0：发送重置密码，1：发送激活码
     * @return boolean
     */
    public static boolean sendEmail(String receiveEmail, String content, String action) {
        // 1.创建一个程序与邮件服务器会话对象 Session
        Properties p = new Properties();
        //p.setProperty("mail.transport.protocol", "SMTP");// 连接协议
        p.setProperty("mail.smtp.host", MEAIL_163_SMTP_HOST);
        p.setProperty("mail.smtp.port", SMTP_163_PORT);
        p.setProperty("mail.smtp.socketFactory.port", SMTP_163_PORT);
        // 指定验证为true
        p.setProperty("mail.smtp.auth", "true");
        //p.setProperty("mail.smtp.timeout","10000");
        p.setProperty("mail.smtp.socketFactory.class", "SSL_FACTORY");

        Session session = Session.getInstance(p, new Authenticator() {
            // 验证账号及密码，密码需要是第三方授权码
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MY_EMAIL_ACCOUNT, MY_EMAIL_PASSWORD);
            }
        });
        session.setDebug(true);

        try {
            // 2.创建一个Message，它相当于是邮件内容
            MimeMessage message = new MimeMessage(session);
            //防止成为垃圾邮件，披上outlook的马甲
            //message.addHeader("X-Mailer","Microsoft Outlook Express 6.00.2900.2869");
            // 设置发送者
            message.setFrom(new InternetAddress(MY_EMAIL_ACCOUNT));
            // 设置发送方式与接收者
            message.setRecipients(Message.RecipientType.TO, receiveEmail);
            // 创建消息主体,有些敏感信息会被邮件服务器禁止发送，所以尽量多测试下
            String subject = "0".equals(action) ? "麻雀博客-重置密码" : "麻雀博客-邮箱激活码";
            message.setSubject(subject);
            String passHtml = "<!DOCTYPE html><head><title>重置密码</title></head><body><html><p>我们重新为您设定的密码是："
                    + "<i style='background: yellow;font-size:14px;'>" + content + "</i>, 请您登录后重置您的密码，谢谢！</p>"
                    + "<p>麻雀博客<br>maque_blogs@163.com</p></body></html>";
            String codeHtml = "<!DOCTYPE html><head><title>激活码</title></head><body><html><p>您的邮箱激活码是："
                    + "<i style='background: yellow;font-size:14px;'>" + content + "</i>, 请您尽快激活邮箱，谢谢！</p>"
                    + "<p>麻雀博客<br>maque_blogs@163.com</p></body></html>";
            message.setContent(("0".equals(action) ? passHtml : codeHtml), "text/html;charset=UTF-8");
            message.setSentDate(new Date());
            message.saveChanges();

            // 3.创建 Transport用于将邮件发送
            Transport.send(message);
        } catch (AddressException e) {
            log.error(e.getMessage());
            return false;
        } catch (MessagingException e) {
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        sendEmail("linlanghen@163.com", "5201314", "0");
    }
}